﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using pwblog.BusinessObject;
using pwblog.Entities;
using pwblog.Common;
using System.IO;

namespace pwblog.Web.Creator
{
    public partial class PostArticle : System.Web.UI.Page
    {
        private ArticleBo articleBo = new ArticleBo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CategoryBo categoryBo = new CategoryBo();
                var categories = categoryBo.GetListItemCategories();
                if (categories.Length > 0)
                {
                    ddlCategory.Items.AddRange(categories);
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                DateTimeFormatInfo dtfiVN = new CultureInfo("vi-VN", false).DateTimeFormat;
                // get info
                string nameArticle = tbNameArticle.Text;
                string image = IThumb.ImageUrl;
                int idCategory = Int32.Parse(ddlCategory.SelectedValue);
                string datePost = DateTime.Now.ToString(dtfiVN);
                DateTime lastModified = DateTime.Now;
                string userName = User.Identity.Name;
                string contents = ckEditorArticle.Text;
                string description = tbDesciption.Text;
                if (String.IsNullOrEmpty(description))
                {
                    description = RegexUtility.stripHtmlTagsUsingHAP(contents).Substring(0, 50);
                }
                // insert dữ liệu bài viết mới vào database
                int idArticle = articleBo.InsertArticle(nameArticle, idCategory, datePost, userName, description, image, lastModified);

                // ghi bai viet tu Editor ra file
                // Ten file dat ten theo Id cua bai viet duoc insert vao database
                string path = Server.MapPath(@"..\Articles\" + idArticle.ToString() + ".txt");
                StreamWriter writer = File.CreateText(path);
                writer.WriteLine(contents);
                writer.Close();

                // tang so bai viet len +1
                new AccountBo().RaiseNumberOfArticle(userName);

                // Chuyen toi trang vua tao xong
                Response.Redirect(@"..\DetailsArticle.aspx?id=" + idArticle.ToString());
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnUpImage_Click(object sender, EventArgs e)
        {
            try
            {
                if (fuThumbs.HasFile)
                {

                    if (CheckFileType(fuThumbs.FileName))
                    {
                        string upFolder = Server.MapPath("~/Web/Articles/thumbs");
                        if (Directory.Exists(upFolder))
                        {
                            string filePath = upFolder + "\\" + fuThumbs.FileName;
                            fuThumbs.SaveAs(filePath);
                            IThumb.ImageUrl = "/Web/Articles/thumbs/" + fuThumbs.FileName;

                            try
                            {
                                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                                System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
                                int fileLength = Convert.ToInt32(fs.Length);
                                lbImageLength.Text = fileLength.ToString();
                                int fileWidth = image.Width;
                                lbImageWidth.Text = fileWidth.ToString();
                                int fileHeight = image.Height;
                                lbImageHeight.Text = fileHeight.ToString();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }

                        }
                    }
                }
            }
            catch { return; }
        }

        private bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }

        }
    }
    
}